function [Y mu evecs evals sigma] = reduceData(X, th, maxD)


[N D] = size(X);
[sigma evecs evals] = ppca(cov(X), D);

varratios = cumsum(evals)/sum(evals);
minD = find((1-varratios) < th);
minD = min(minD(1), maxD);

[sigma evecs evals] = ppca(cov(X), minD);

mu = mean(X);
X_mu = X - ones(N, 1)*mu;

Y = X_mu*evecs;
